<?php
session_start();
/**
 * ===== groups
 * root = 1
 * ====/ groups
 */
if(
	(!empty($_SESSION["Auth"]["User"]["group_id"])) 
		&& 
	(
		(($_SESSION["Auth"]["User"]["group_id"] == 1) || ($_SESSION["Auth"]["User"]["group_id"] == 3))
	)
) {
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Медиа Мениджър :: качване</title>
</head>
<body>
<a href="#" onClick="javascript:window.close()" class="menu">затвори този прозорец</a><br />
<br />
<?php
	if(empty($_REQUEST['dir']) || empty($_REQUEST['id'])) {
		echo 'Error: empty dir or id';
		echo '
		</body>
		</html>
		';
		exit;
	}
?>

<form action="tinymce_upload.php" method="POST" enctype="multipart/form-data">
Качете файл:<br />
<input type="file" name="userfile" /><br />

<input type="hidden" name="dir" value="<?php echo $_REQUEST['dir']; ?>" />
<input type="hidden" name="id" value="<?php echo $_REQUEST['id']; ?>" />

<input type="submit" name="submit" value="Изпрати" />
</form>
</body>
</html>
<?php
if($_FILES['userfile']) {
	$blankIndexFile = '../../files/index.html';
	
	//--------------------------------------- PATH OF DIR
	@ $url_dirMain = $_POST['dir'];
	@ $url_dirId = $_POST['id'];
	$root_dir = '../../files/site_data/tinymce_uploads/';
	$final_dir = '';
	
	if(!file_exists(realpath($root_dir))) {
		mkdir($root_dir, 0755);
		chmod($root_dir, 0755);
		
		copy($blankIndexFile, $root_dir.'index.html');
	}

	if($url_dirMain == 'categories') {
		$categories_dir = $root_dir.'categories/';
		if(!file_exists(realpath($categories_dir))) {
			mkdir($categories_dir, 0755);
			chmod($categories_dir, 0755);
			
			copy($blankIndexFile, $categories_dir.'index.html');
		}
		
		$final_dir = '/categories/'.$url_dirId.'/';
	} else if($url_dirMain == 'locations') {
		$locations_dir = $root_dir.'locations/';
		if(!file_exists(realpath($locations_dir))) {
			mkdir($locations_dir, 0755);
			chmod($locations_dir, 0755);
			
			copy($blankIndexFile, $locations_dir.'index.html');
		}
		
		$final_dir = '/locations/'.$url_dirId.'/';
	} else {
		$final_dir = '/';
	}
	//--------------------------------------/ PATH OF DIR
	
	$userfile_name = $_FILES['userfile']['name'];
	$userfile_type = $_FILES['userfile']['type'];
	$userfile_size = $_FILES['userfile']['size'];
	$userfile_tmpname = $_FILES['userfile']['tmp_name'];
	$userfile_error = $_FILES['userfile']['error'];
	
	$up_dir = '../../files/site_data/tinymce_uploads'.$final_dir;
	if(!file_exists(realpath($up_dir))) {
		mkdir($up_dir, 0755);
		chmod($up_dir, 0755);
		
		copy($blankIndexFile, $up_dir.'index.html');
	}
	
	$up_file = $up_dir.$userfile_name;
	
	$i = 0;
	while(file_exists($up_file)) {
		$i++;
		$up_file = $up_dir.'__'.$i.'__'.$userfile_name;
	}
	
	if(is_uploaded_file($userfile_tmpname)) {
		move_uploaded_file($userfile_tmpname, $up_file);
		chmod($up_file, 0644);
		echo 'Качено успешно.';
	} else {
		die("ГРЕШКА при качването на файла");
	}
	
}
?>

<?php
} else {
echo 'Login first...';
}
?>